//
//  PictureViewController.m
//  GameBoss
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PictureViewController.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "AFN_Get.h"
#import "PictureModel.h"
#import "PictureCollectionCell.h"
#import "PictureDataViewController.h"
#import "NightMode.h"
#import "NightManager.h"
#import "UIScrollView+MJRefresh.h"

@interface PictureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)UICollectionView *picCollection;
@property(nonatomic, retain)UISegmentedControl *picSeg;
@property(nonatomic, retain)NSString *strSeg;

@end

@implementation PictureViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"图片";
        self.picArr = [NSMutableArray array];
    }
    return self;
}

-(void)picData:(NSString *)strTemp{
    
    NSString *strURL = [NSString stringWithFormat:@"http://agent1.pconline.com.cn:8042/phlgame/iphone_cate_json.jsp?id=%@", strTemp];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *arr = [NSMutableArray array];
        arr = responseObject[@"groups"];
        self.picArr = [NSMutableArray array];
        for (NSDictionary *temp in arr) {
            PictureModel *model = [[PictureModel alloc] init];
            [model setValuesForKeysWithDictionary:temp];
            [self.picArr addObject:model];
            [model release];
        }
        [self.picCollection reloadData];
#pragma mark 结束刷新
        [self.picCollection headerEndRefreshing];
        
        if (self.picSeg.selectedSegmentIndex == 0) {
            [[DataCache shareDataCache] dropTable:@"pictureTable"];
            [[DataCache shareDataCache] createTable:@"pictureTable"];
            for (int i=0; i<self.picArr.count; i++) {
                [[DataCache shareDataCache] insertModel:@"pictureTable" model:self.picArr[i]];
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"解析失败");
        NSLog(@"error:%@", error);
#pragma mark 结束刷新
        [self.picCollection headerEndRefreshing];
        if (self.picSeg.selectedSegmentIndex == 0) {
            self.picArr = [[DataCache shareDataCache] selectModel:@"pictureTable"];
            [self.picCollection reloadData];
        }
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height)];
    imageView.image = [UIImage imageNamed:@"zaochen"];
    [self.view addSubview:imageView];
    [imageView release];
    
    self.tabBarController.tabBar.hidden = NO;
    
    //设置seg
    self.picSeg = [[UISegmentedControl alloc] initWithItems:@[@"游戏美女", @"游戏美图"]];
    self.picSeg.frame = CGRectMake(0, 0, Screen.width, 40);
    [self.view addSubview:self.picSeg];
    //默认选择为0;
    self.picSeg.selectedSegmentIndex = 0;
    //加渲染色
    self.picSeg.tintColor =[UIColor whiteColor];
    self.picSeg.backgroundColor = [UIColor colorWithRed:237/255.0 green:200/255.0 blue:0/255.0 alpha:1];
    
    //flowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(Screen.width * 110/375, Screen.height * 110/667);
    flowLayout.sectionInset = UIEdgeInsetsMake(Screen.width * 10/375, Screen.height * 10/667, Screen.width * 10/375, Screen.height * 10/667);
    
    self.picCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, Screen.width, Screen.height - 153) collectionViewLayout:flowLayout];
    self.picCollection.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.picCollection];
    self.picCollection.dataSource = self;
    self.picCollection.delegate = self;
    
    [self.picCollection registerClass:[PictureCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
#pragma mark 夜间判断
    if ([NightMode shareNightMode].isNight) {
        [[NightMode shareNightMode] loadNightModeInfoFormDisk];
    }

    //刷新
    [self addHeader];
    [self picData:@"3307"];
    
#pragma mark 选择方法写在后面
    [self.picSeg addTarget:self action:@selector(picSegClick:) forControlEvents:UIControlEventValueChanged];
    [_picSeg release];
}

-(void)picSegClick:(UISegmentedControl *)picSeg{
    switch (picSeg.selectedSegmentIndex) {
        case 0:
            [self picData:@"3307"];
            break;
        case 1:
            [self picData:@"3308"];
            break;
        default:
            break;
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return self.picArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PictureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    [cell.picImage sd_setImageWithURL:[NSURL URLWithString:[self.picArr[indexPath.row] cover]] placeholderImage:[UIImage imageNamed:@"iconfont-yonghutouxiang"]];
    cell.iconPic.image = [UIImage imageNamed:@"iconfont-tupianji"];
    cell.picCount.text = [NSString stringWithFormat:@"%ld", [self.picArr[indexPath.row] photoCount]];
    return cell;
}
#pragma mark collection点击传值
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    PictureDataViewController *picDataVC = [[PictureDataViewController alloc] init];
    
    PictureModel *model = self.picArr[indexPath.row];
    picDataVC.picUrl = model.url;
#pragma mark 下一页的tabBar消失
    picDataVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:picDataVC animated:YES];
    [picDataVC release];
}
#pragma mark pic刷新
- (void)addHeader
{
    [self.picCollection addHeaderWithCallback:^{
        if (self.picSeg.selectedSegmentIndex == 0) {
            [self picData:@"3307"];
        }else{
            [self picData:@"3308"];
        };
    }];
#pragma mark 第一次开始,刷新写
    [self.picCollection headerBeginRefreshing];
}


#pragma mark 选取白黑两种模式
- (void)setNightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
}

- (void)setLightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_picArr release];
    [_picCollection release];
    [_picSeg release];
    [super dealloc];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
